package com.yxjx.duoxue.my;
import java.io.IOException;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;

import com.alipay.sdk.data.Response;
import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.customview.YxListView;
import com.yxjx.duoxue.datamodel.ResponseObject;
import com.yxjx.duoxue.network.MsgCode;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.network.UserReg;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;


public class SatisficationEstimationActivity extends BaseActionBarActivity {
	
	private String[] mItems;
	private YxListView mList;
	private int mSelected = -1;
	private boolean mHanding = false; // when handing, dismiss another submit.

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_satisfication_estimation);
		customizeActionBarTitle("满意度调查");
		
		mItems = getResources().getStringArray(R.array.satis_estim_array);
		mList = (YxListView) findViewById(R.id.list);
		mList.setAdapter(new MyAdapter());
		
		
		mList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				for (int i = 0; i < arg0.getChildCount(); i++) {
					if (i != arg2) {
						mList.getChildAt(i).findViewById(R.id.indicator).setVisibility(View.INVISIBLE);
						mList.getChildAt(i).findViewById(R.id.text).setSelected(false);
					}
				}
				mList.getChildAt(arg2).findViewById(R.id.text).setSelected(true);
				mList.getChildAt(arg2).findViewById(R.id.indicator).setVisibility(View.VISIBLE);
				mSelected = arg2;
			}
		});
		UIHelper.setClickListener(findViewById(R.id.root), R.id.submit, mClickListener);
	}
	
	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mItems.length;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (arg1 == null) {
				ret = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_satisf_list, null);
			} else {
				ret = arg1;
			}
			UIHelper.setText(ret, R.id.text, mItems[arg0]);
			return ret;
		}
	}
	
	private View.OnClickListener mClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(final View arg0) {
			if (mHanding) {
				return;
			}
			
			if (mSelected == -1) {
				UIHelper.showTips(arg0, "请选择推荐值");
				return;
			}
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					mHanding = true;
					ResponseObject code = new ResponseObject(-1, "");
					int grade = (10 - mSelected);
					
					try {
						code = NwUtils.submitNps(getApplicationContext(), "" + grade);
					} catch (ClientProtocolException e) {
						e.printStackTrace();
					} catch (ParseException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} catch (JSONException e) {
						e.printStackTrace();
					}
					
					UIHelper.showTips(arg0, code.getCode() == MsgCode.SUCCESS ? "提交成功" : "提交失败，" + MsgCode.descFor(code));
					if (code.getCode() == MsgCode.SUCCESS) {
						SatisficationEstimationActivity.this.finish();
					} else {
						mHanding = false;
					}
					
				}
			}).start();
			
		}
	};
	
	

}
